package com.ruoyi.web.controller.biz;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.domain.BizDutyType;
import com.ruoyi.biz.service.IBizDutyTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 值班班次类型Controller
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@Controller
@RequestMapping("/biz/type")
public class BizDutyTypeController extends BaseController
{
    private String prefix = "biz/type";

    @Autowired
    private IBizDutyTypeService bizDutyTypeService;

    @RequiresPermissions("biz:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询值班班次类型列表
     */
    @RequiresPermissions("biz:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizDutyType bizDutyType)
    {
        startPage();
        Long              userId = ShiroUtils.getUserId();
        bizDutyType.setUserId(userId.intValue());
        List<BizDutyType> list = bizDutyTypeService.selectBizDutyTypeList(bizDutyType);
        return getDataTable(list);
    }

    @GetMapping("/getTypes")
    @ResponseBody
    public Object getTypes()
    {
        Long              userId = ShiroUtils.getUserId();
        BizDutyType bizDutyType = new BizDutyType();
        bizDutyType.setUserId(userId.intValue());
        List<BizDutyType> list = bizDutyTypeService.selectBizDutyTypeList(bizDutyType);
        return AjaxResult.success(list);
    }

    /**
     * 导出值班班次类型列表
     */
    @RequiresPermissions("biz:type:export")
    @Log(title = "值班班次类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizDutyType bizDutyType)
    {
        List<BizDutyType> list = bizDutyTypeService.selectBizDutyTypeList(bizDutyType);
        ExcelUtil<BizDutyType> util = new ExcelUtil<BizDutyType>(BizDutyType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增值班班次类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存值班班次类型
     */
    //@RequiresPermissions("biz:type:add")
    @Log(title = "值班班次类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizDutyType bizDutyType)
    {
        Long              userId = ShiroUtils.getUserId();
        bizDutyType.setUserId(userId.intValue());
        bizDutyType.setCreateBy(userId.toString());

        BizDutyType param = new BizDutyType();
        param.setDutyTypeName(bizDutyType.getDutyTypeName());
        param.setUserId(userId.intValue());
        Integer count = bizDutyTypeService.checkIfExist(param);
        if (count!=null && count > 0) {
            return AjaxResult.error("已存在");
        }

        return toAjax(bizDutyTypeService.insertBizDutyType(bizDutyType));
    }

    /**
     * 修改值班班次类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        BizDutyType bizDutyType = bizDutyTypeService.selectBizDutyTypeById(id);
        mmap.put("bizDutyType", bizDutyType);
        return prefix + "/edit";
    }

    /**
     * 修改保存值班班次类型
     */
    @RequiresPermissions("biz:type:edit")
    @Log(title = "值班班次类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizDutyType bizDutyType)
    {
        Long              userId = ShiroUtils.getUserId();
        bizDutyType.setUpdateBy(userId.toString());
        return toAjax(bizDutyTypeService.updateBizDutyType(bizDutyType));
    }

    /**
     * 删除值班班次类型
     */
    @RequiresPermissions("biz:type:remove")
    @Log(title = "值班班次类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizDutyTypeService.deleteBizDutyTypeByIds(ids));
    }
}
